/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.data.tables;

import android.database.Cursor;

import id.duapuluhdua.dakidaki.UserAlert.Result;
import id.duapuluhdua.dakidaki.data.common.DataColumn;
import id.duapuluhdua.dakidaki.data.common.DataTable;
import id.duapuluhdua.dakidaki.data.common.DataValues;
import id.duapuluhdua.dakidaki.data.common.Database;
import id.duapuluhdua.dakidaki.data.items.DataItemVersion;

/**
 *
 */
public class DataTableVersion extends DataTable
{
  /** Constants */
  public final static String tableName = "Version";

  /** columns */
  public static class column
  {
    public static final int ID = 0;
    public static final int TableName = 1;
    public static final int Version = 2;
  };

  private final static DataColumn[] vecTableDefinition =
  {
    new DataColumn(column.ID, DataTable.COL_ID, DataColumn.TYPE_INT, false, true, false),
    new DataColumn(column.TableName, "Lon", DataColumn.TYPE_TEXT, false, false, false),
    new DataColumn(column.Version, "Lat", DataColumn.TYPE_INT, false, false, false),
  };

  /**
   * Default constructor.
   *
   * @param database the database object.
   */
  public DataTableVersion(Database database)
  {
    super(database, tableName, vecTableDefinition);
//      rows = new RowSet(this);
  }

  @Override
  public DataValues getNewValues(boolean forInsert)
  {
    final DataValues values = new DataValues(this);

    if (forInsert) {
      // Do nothing.
    }
    return values;
  }

  public boolean setVersion(String tableName, int version)
  {
    boolean bSuccess = false;

    if (tableName.equals(tableName)) {
      return true;
    }

    final DataItemVersion item = getVersionItem(tableName);
    if (item == null) {
      DataValues values = new DataValues(this);
      values.setValue(column.TableName, tableName);
      values.setValue(column.Version, version);
      bSuccess = (dataInsert(values) != -1);
    }
    else {
      DataValues values = new DataValues(this);
      values.setValue(column.TableName, tableName);
      values.setValue(column.Version, version);
      bSuccess = dataUpdate(values, item.id);
    }

    return bSuccess;
  }

  /**
   * Retrieves the version item for a particular table.
   *
   * @param tableName the table name.
   * @return the data item.
   */
  private DataItemVersion getVersionItem(String tableName)
  {
    final String where = String.format(
        "%s = \"%s\"", getColumnName(column.TableName), tableName);

    final Cursor cr = getDataRecords(where);
    if (cr != null) {
      DataItemVersion item = new DataItemVersion(cr);
      cr.close();
      return item;
    }
    return null;
  }

  /**
   * Gets the current version of table structure.
   *
   * @return a version number.
   */
  @Override
  public int getTableVersion()
  {
    return 1;
  }

  @Override
  public Result validateValues(DataValues values)
  {
    return null;  // TODO assume all OK.
  }
}
